package com.ruoyi.system.service.sz.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.sz.*;
import com.ruoyi.system.mapper.sz.*;
import com.ruoyi.system.service.sz.ISzTimeLineService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.constant.TimeLineConstants.TIME_TYPE_0;

/**
 * 时间轴Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-16
 */
@Service
public class SzTimeLineServiceImpl implements ISzTimeLineService
{
    @Autowired
    private SzTimeLineMapper szTimeLineMapper;
    @Autowired
    private SzLeaderGroupMapper szLeaderGroupMapper;
    @Autowired
    private SzTimeFileMapper szTimeFileMapper;
    @Autowired
    private SzTimeFileSnapshotMapper szTimeFileSnapshotMapper;
    @Autowired
    private SzLeaderGroupSnapshotMapper szLeaderGroupSnapshotMapper;


    /**
     * 查询时间轴
     * 
     * @param timeId 时间轴主键
     * @return 时间轴
     */
    @Override
    public SzTimeLine selectSzTimeLineByTimeId(Long timeId)
    {
        SzTimeLine szTimeLine = szTimeLineMapper.selectSzTimeLineByTimeId(timeId);
        setLineFileAndLeader(szTimeLine);
        return szTimeLine;
    }

    /**
     * 查询时间轴列表
     * 
     * @param szTimeLine 时间轴
     * @return 时间轴
     */
    @Override
    public List<SzTimeLine> selectSzTimeLineList(SzTimeLine szTimeLine)
    {
        List<SzTimeLine> szTimeLines = szTimeLineMapper.selectSzTimeLineList(szTimeLine);
        for(SzTimeLine line : szTimeLines){
            setLineFileAndLeader(line);
        }
        return szTimeLines;
    }

    private void setLineFileAndLeader(SzTimeLine line) {
        String timeType = line.getTimeType();
        Long[] fileCollect = null;
        if(StringUtils.isNotBlank(line.getFileIds())) {
            fileCollect = Arrays.stream(line.getFileIds().split(",")).map(Long::parseLong).collect(Collectors.toList()).stream().toArray(Long[]::new);
        }
        Long[] groupCollect = null;
        if(StringUtils.isNotBlank(line.getGroupIds())) {
            groupCollect = Arrays.stream(line.getGroupIds().split(",")).map(Long::parseLong).collect(Collectors.toList()).stream().toArray(Long[]::new);
        }

        if(TIME_TYPE_0.equals(timeType)){
            if(null != fileCollect) {
                List<SzTimeFile> szTimeFiles = szTimeFileMapper.selectSzTimeFileByFileIds(fileCollect);
                line.setSzTimeFileList(szTimeFiles);
            }
            if(null != groupCollect) {
                List<SzLeaderGroup> szLeaderGroups = szLeaderGroupMapper.selectSzLeaderGroupByGroupIds(groupCollect);
                line.setSzLeaderGroupList(szLeaderGroups);
            }
        }else{
            if(null != groupCollect) {
                List<SzLeaderGroup> collect = szLeaderGroupSnapshotMapper.selectSzLeaderGroupSnapshotBySnapshotIds(groupCollect).stream().map(group -> {
                    SzLeaderGroup groupSnapshot = new SzLeaderGroup();
                    groupSnapshot.setGroupId(group.getGroupId());
                    groupSnapshot.setGroupType(group.getGroupType());
                    groupSnapshot.setDepartment(group.getDepartment());
                    groupSnapshot.setName(group.getName());
                    groupSnapshot.setPhone(group.getPhone());
                    groupSnapshot.setPosition(group.getPosition());
                    groupSnapshot.setPositionName(group.getPositionName());
                    groupSnapshot.setRbName(group.getRbName());
                    groupSnapshot.setDeptName(group.getDeptName());
                    groupSnapshot.setSubDeptName(group.getSubDeptName());
                    groupSnapshot.setTitleName(group.getTitleName());
                    groupSnapshot.setResponsibility(group.getResponsibility());
                    groupSnapshot.setTitle(group.getTitle());
                    groupSnapshot.setSubDepartment(group.getSubDepartment());
                    return groupSnapshot;
                }).collect(Collectors.toList());
                line.setSzLeaderGroupList(collect);
            }
            if(null != fileCollect) {
                List<SzTimeFile> collect1 = szTimeFileSnapshotMapper.selectSzTimeFileSnapshotBySnapshotIds(fileCollect).stream().map(file -> {
                    SzTimeFile fileSnapshot = new SzTimeFile();
                    fileSnapshot.setFileContent(file.getFileContent());
                    fileSnapshot.setFileId(file.getFileId());
                    fileSnapshot.setFileName(file.getFileName());
                    fileSnapshot.setFileType(file.getFileType());
                    return fileSnapshot;
                }).collect(Collectors.toList());
                line.setSzTimeFileList(collect1);
            }
        }
    }

    /**
     * 新增时间轴
     * 
     * @param szTimeLine 时间轴
     * @return 结果
     */
    @Override
    public int insertSzTimeLine(SzTimeLine szTimeLine)
    {
        szTimeLine.setCreateTime(DateUtils.getNowDate());
        return szTimeLineMapper.insertSzTimeLine(szTimeLine);
    }

    /**
     * 修改时间轴
     * 
     * @param szTimeLine 时间轴
     * @return 结果
     */
    @Override
    public int updateSzTimeLine(SzTimeLine szTimeLine)
    {
        szTimeLine.setUpdateTime(DateUtils.getNowDate());
        return szTimeLineMapper.updateSzTimeLine(szTimeLine);
    }

    /**
     * 批量删除时间轴
     * 
     * @param timeIds 需要删除的时间轴主键
     * @return 结果
     */
    @Override
    public int deleteSzTimeLineByTimeIds(Long[] timeIds)
    {
        return szTimeLineMapper.deleteSzTimeLineByTimeIds(timeIds);
    }

    /**
     * 删除时间轴信息
     * 
     * @param timeId 时间轴主键
     * @return 结果
     */
    @Override
    public int deleteSzTimeLineByTimeId(Long timeId)
    {
        return szTimeLineMapper.deleteSzTimeLineByTimeId(timeId);
    }
}
